package com.curry.apiprovider.service.impl;

import com.curry.apiprovider.service.RedisCacheDao;
import com.curry.apiprovider.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

@Service
public class RedisService implements RedisCacheDao {

    @Autowired
    private RedisTemplate redisTemplate;


    public <T> void setRedisTemplateObj(String key, List<T> list) {
        redisTemplate.opsForValue().set(key, list);
    }

    public void setRedisTemplateObj(String key, Object object, Long expires) {
        redisTemplate.opsForValue().set(key, object, expires);
    }

    public <T> List<T> getRedisTemplateObj(String key) {
        List<T> t = (List<T>) redisTemplate.opsForValue().get(key);
        return t;
    }

    public void saveMsg(String key, String obj) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, obj);
    }

    public void saveMsg(String key, String obj, long expaire) {
        redisTemplate.opsForValue().set(key, obj, expaire);
    }

    public String getMsg(String key) {
        if (redisTemplate.hasKey(key)) {
            ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
            return valueOperations.get(key);
        }
        return "";
    }

    public String redisKey(String keyword, String cityCode, Integer channelId, Integer pageIndex, Integer pageSize) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(keyword);
        stringBuffer.append(cityCode);
        stringBuffer.append(channelId);
        stringBuffer.append(pageIndex);
        stringBuffer.append(pageSize);
        return MD5Util.MD5Encode(stringBuffer.toString().trim(), "utf-8");
    }

    @Override
    public <T> void save(String key, T obj) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        operations.set(key, obj);
    }

    @Override
    public <T> void save(String key, T obj, Long expires) {
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        operations.set(key, obj, expires);
    }

    @Override
    public <T> void pushSetItem(String key, T obj) {
        SetOperations<Serializable, Object> operations = redisTemplate.opsForSet();
        operations.add(key,obj);
    }

    @Override
    public <T> void pushSetItem(String key, T obj, Long expires) {
        SetOperations<Serializable, Object> operations = redisTemplate.opsForSet();
        operations.add(key, obj, expires);
    }

    @Override
    public <T> void removeSetItem(String key, T obj) {

    }

    @Override
    public <T> T read(String key, Class<T> type) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        return (T) valueOperations.get(key);
    }

    @Override
    public <T> void delete(String key, Class<T> type) {
        if (redisTemplate.hasKey(key)) {
            redisTemplate.delete(key);
        }
    }

    @Override
    public <T> Set<T> readSet(String key, Class<T> type) {
        Object pop = redisTemplate.opsForSet().pop(key);
        return (Set<T>) pop;
    }

    @Override
    public <T> void pushLisItem(String key, T obj) {
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll(key, obj);
    }

    @Override
    public <T> void pushListItem(String key, T obj, Long expires) {
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll(key, obj);
    }

    @Override
    public <T> void removeListItem(String key, T obj) {

    }

    @Override
    public <T> List<T> readList(String key, Class<T> type) {
        return (List<T>) redisTemplate.opsForList().leftPop(key);
    }

    @Override
    public <T> Long incr(String key, Class<T> type) {
        return null;
    }

    @Override
    public <T> Long incrBy(String key, Long integer, Class<T> type) {
        return null;
    }
}
